<template>
  <!-- 新增/编辑字典详情 -->
  <el-dialog :title="dialogTitle" v-model="dictDetailParams.visible" @open="handleOpen" width="500"
    :close-on-click-modal="false">
    <div class="dict-main" v-loading="loading">
      <el-form ref="ruleFormRef" :model="formData" :rules="rules" label-width="100">
        <el-form-item label="字典标签：" prop="itemName">
          <el-input v-model.trim="formData.itemName" maxlength="60" clearable placeholder="请输入字典标签" />
        </el-form-item>
        <el-form-item label="字典值：" prop="itemCode">
          <el-input v-model.trim="formData.itemCode" maxlength="60" clearable placeholder="请输入字典值" />
        </el-form-item>
        <el-form-item label="状态：" prop="enable">
          <el-radio-group v-model="formData.enable">
            <el-radio v-for="(value, key, index) in STATUS_TYPE" :value="Number(key)" :key="index">{{ value
              }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="标签类型：" prop="tagType">
          <el-radio-group v-model="formData.tagType" class="tag-radios">
            <el-radio v-for="(item, index) in TAG_TYPE" :value="item.value" :key="index" border size="small">{{
              item.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="标签示例：" v-if="formData.tagType">
          <el-tag :type="formData.tagType"> {{ formData.itemName }}</el-tag>
        </el-form-item>
      </el-form>
    </div>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="emit('close')">返回</el-button>
        <el-button type="primary" :disabled="loading" @click="handleSubmit">提交</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { reactive, ref, nextTick, computed } from 'vue'
import { ElMessage } from 'element-plus'
import { useRoute } from 'vue-router'
import { addDataDict, modifyDataDict } from '@views/dictManage/api/dictManage'
import { STATUS_TYPE, TAG_TYPE } from '@constants/dictionary'
const props = defineProps({
  dictDetailParams: {
    type: Object,
    default: () => { },
    required: true
  }
})

const route = useRoute()
const emit = defineEmits(['close'])
const formData = reactive({ enable: 1, tagType: '' })
const ruleFormRef = ref(null)
const loading = ref(false)
const dialogTitle = computed(() => props.dictDetailParams.row?.id ? '编辑字典数据' : '新增字典数据')
const rules = reactive({
  itemName: [{ required: true, message: '请输入字典标签', trigger: 'blur' }],
  itemCode: [{ required: true, message: '请输入字典值', trigger: 'blur' }],
  enable: [{ required: true, message: '请选择状态', trigger: 'change' }]
})

const handleSubmit = () => {
  ruleFormRef.value.validate((valid) => {
    if (valid) {
      handleSave()
    } else {
      return false
    }
  })
}
const handleSave = async () => {
  loading.value = true
  const { query } = { ...route }
  const res = formData.id ? await modifyDataDict(formData) : await addDataDict({ classifyCode: query.classifyCode, ...formData })
  loading.value = false
  if (res?.code === 200) {
    ElMessage.success('保存成功')
    emit('close', true)
  }
}
// 初始化弹窗
const handleOpen = () => {
  nextTick(() => {
    ruleFormRef.value.resetFields()

    let row = props.dictDetailParams.row
    if (row.id) {
      // 编辑
      formData.id = row.id
      formData.itemName = row.itemName
      formData.itemCode = row.itemCode
      formData.enable = row.enable
      formData.tagType = row.tagType
    } else {
      // 新增
      formData.id = ''
    }
    console.log('初始化')
  })
}

</script>

<style lang="scss" scoped>
.dict-main {

  .tag-radios {
    .el-radio-group {
      display: flex;
      gap: 10px
    }

    .el-radio {
      width: 95px;
      margin-right: 0;
    }
  }
}
</style>
